fix(token-lists): drop tokens for chains absent from viem/chains#470
Merged
Conversation
@uniswap/default-token-list still ships tokens for Ropsten (chainId 3) and Rinkeby (chainId 4), both removed from viem/chains. combineTokenLists caught the buildNativeToken throw but logged it via console.error before ignoring it, producing noise on every cold load. Extend the existing safeParse filter to also drop tokens whose chainId is not present in viem/chains, mirroring the existing non-EVM drop. This removes the throw at source and keeps tokensByChainId free of unreachable buckets. Closes #465
…nsById Map Eliminates the double enumeration of viem/chains: the Set built for filter membership and the Object.values().find() in buildNativeToken both traversed the same data. A single module-level Map keyed by chain id serves both lookups, making buildNativeToken an O(1) get instead of O(n) find. Also flattens the two-statement filter predicate into a single && expression and condenses the 7-line inline comment to the essential WHY.
…p dead catch viem/chains exports 702 chain objects spanning 675 unique IDs; 27 export names share a chainId, and 10 of those collisions have divergent nativeCurrency. The previous Map(...map) construction was last-wins, silently changing buildNativeToken results versus the original find()-first-wins behaviour for chainIds 9, 166, 260, 999, 1001, 1337, 8217, 9496, 200810, 200901 -- most consequentially chainId 1337 (Localhost ETH/18 → Tempo USD/6). Replace with an explicit first-wins loop. Hoist chainsById to the top of the module so both the filter in combineTokenLists and buildNativeToken reference the same declaration without a forward-reference. After the chainsById.has(token.chainId) filter, every token entering the reduce accumulator has a known chain, making the try/catch/console.error branch unreachable. Remove it; the throw inside buildNativeToken remains as a defensive invariant for callers that bypass the filter. Add a positive-survival assertion to the unsupported-chainId test to guard against future filter inversions silently dropping valid tokens.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR removes noisy console errors caused by deprecated testnet tokens (e.g., chainIds 3 and 4) still present in @uniswap/default-token-list, by filtering out tokens whose chainId is not available in viem/chains before native-token injection occurs.
Changes:
- Introduces a shared
chainsByIdmap for O(1) chain lookups and reuse across token processing. - Filters out tokens whose
chainIdis absent fromviem/chains, preventingbuildNativeTokenfrom throwing. - Updates tests to cover the unsupported-
chainIdfiltering behavior and ensure no logging occurs.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/hooks/useTokenLists.ts | Adds chainsById, filters unsupported chain IDs early, and simplifies native-token injection by removing the now-unreachable catch path. |
| src/hooks/useTokenLists.test.ts | Adds a regression test ensuring unsupported chain IDs are filtered out and no console.error is emitted. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #465
@uniswap/default-token-liststill ships tokens for deprecated testnets (Ropsten chain ID 3, Rinkeby chain ID 4) thatviem/chainsno longer exports. On every cold load,combineTokenListscaught thebuildNativeTokenthrow and emitted it viaconsole.error-- noise despite the intent to ignore it. This implements Option B from the issue: filter out tokens whosechainIdis absent fromviem/chainsbefore they reachbuildNativeToken, eliminating the errors at source and keepingtokensByChainIdfree of unreachable chain buckets.Changes
chainIdvalues out of combined token lists before building native tokenssupportedChainIdsSet with a sharedchainsByIdMap for O(1) lookups and reuse across the pipelinechainsByIdMap key asnumberto satisfy tscchainsByIdpopulation and drop the now-dead catch blockAcceptance criteria
Native token not founderrors logged when loading the app with default configtokensByChainIdcontains only chains supported byviem/chainspnpm lint,pnpm test,pnpm buildall passTest plan
Automated tests
src/hooks/useTokenLists.test.ts-- updated with coverage for the unsupported-chainId filter case.Run:
pnpm test src/hooks/useTokenLists.test.tsManual verification
pnpm devuseTokenLists)Native token not found for chain ID: 3or... chain ID: 4errors appear on cold loadBreaking changes
None.
Checklist
Screenshots
None.